<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>使用远程文件</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="features.file-upload.errors.seealso.html">« 参见</a></li>
      <li style="float: right;"><a href="features.connection-handling.html">连接处理 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="features.html">特点</a></li>
    <li>使用远程文件</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="features.remote-files" class="chapter">
 <h1>使用远程文件</h1>


 <p class="para">
  只要在 <var class="filename">php.ini</var> 中启用 <strong class="option unknown">allow_url_fopen</strong>，就可以将 <abbr title="Hypertext Transfer Protocol">HTTP</abbr>
  和 <abbr title="File Transfer Protocol">FTP</abbr> URL 与大多数以文件名作为参数的函数一起使用。此外，也可以在
  <span class="function"><a href="function.include.html" class="function">include</a></span>、<span class="function"><a href="function.include-once.html" class="function">include_once</a></span>、<span class="function"><a href="function.require.html" class="function">require</a></span>
  及 <span class="function"><a href="function.require-once.html" class="function">require_once</a></span> 语句中使用 URL（必须启用
  <strong class="option unknown">allow_url_include</strong>）。PHP 协议支持的更多信息参见<a href="wrappers.html" class="xref">支持的协议和封装协议</a>。
 </p>
 <p class="para">
  例如，可以用此打开远程 web
  服务器上的文件，解析输出以获取所需数据，然后在数据库查询中使用该数据，或者网站其余部分相同的样式输出内容。
 </p>
 <p class="para">
  <div class="example" id="example-458">
   <p><strong>示例 #1 获取远程页面的标题</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$file</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;p&gt;Unable&nbsp;to&nbsp;open&nbsp;remote&nbsp;file.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br />while&nbsp;(!</span><span style="color: #0000BB">feof&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$line&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fgets&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1024</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;仅当标题跟标签在同一行时才有效&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">preg_match&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"@\&lt;title\&gt;(.*)\&lt;/title\&gt;@i"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$line</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$title&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 <p class="para">
  也可以在 FTP 
  服务器上写入文件（提供具有正确访问权限的用户身份连接）。只能使用此方法创建新文件；如果尝试覆盖已存在的文件，则调用
  <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> 将失败。
 </p>
 <p class="para">
  要以“anonymous”以外的用户名连接服务器，需要指明用户名（可能还有密码），例如“<code class="literal">ftp://user:password@ftp.example.com/path/to/file</code>”（当需要基础认证的
  HTTP 协议访问远程文件时也可以使用相同的语法）。
 </p>
 <p class="para">
  <div class="example" id="example-459">
   <p><strong>示例 #2 将数据保存到远程服务器</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"ftp://ftp.example.com/incoming/outputfile"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$file</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;p&gt;Unable&nbsp;to&nbsp;open&nbsp;remote&nbsp;file&nbsp;for&nbsp;writing.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /></span><span style="color: #FF8000">/*&nbsp;这里写入数据。&nbsp;*/<br /></span><span style="color: #0000BB">fwrite&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'HTTP_USER_AGENT'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 <p class="para">
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    或许可以从以上范例中了解到可以使用该技术写入远程日志文件。但不幸的是，这不起作用，因为如果如果远程文件已存在，调用 
    <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> 将失败。要进行这样的分布式日志记录，应该参考 <span class="function"><a href="function.syslog.html" class="function">syslog()</a></span>。
   </p>
  </p></blockquote>
 </p>

</div>
</div></div></body></html>